package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.Collections;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Prints the power generated (or consumed) by the source at the given day.
 * 
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotListPowerStatsCommand extends WattDepotCommand {

  /**
   * Prints the power generated (or consumed) by the source at the given day.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string of power generated.
   */
  public String doCommand(String[] args) {

    if (args.length != 9 || 0 != args[5].compareTo("sampling-interval")
        || 0 != args[7].compareTo("statistic") || 0 != args[3].compareTo("day")) {
      return "powerstats [generated|consumed] {source} day {day} sampling-interval {minutes} "
          + "statistic {max|min|average}";
    }

    return getPowerStats(args);
  }

  /**
   * Gets the power generated (or consumed) by the source at the given day.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string variable named output containing all the output for the method.
   */
  private String getPowerStats(String[] args) {
    boolean error = false;
    String output = "";
    ArrayList<Double> powerArray = new ArrayList<Double>();
    double totalPower = 0;
    double powerVal = 0;
    XMLGregorianCalendar startTS = null;
    XMLGregorianCalendar endTS = null;

    try {
      startTS = Tstamp.makeTimestamp(args[4]);
      endTS = Tstamp.incrementDays(startTS, 1);
    }
    catch (Exception e) {
      return "Day is not properly formatted.";
    }

    try {
      while (startTS.getDay() < endTS.getDay()) {
        if (0 == args[1].compareTo("generated")) {
          powerVal = getClient().getPowerGenerated(args[2], startTS) / 1000000;
        }
        else if (0 == args[1].compareTo("consumed")) {
          powerVal = getClient().getPowerConsumed(args[2], startTS) / 1000000;
        }
        else {
          return BAD_FORMAT;
        }
        totalPower += powerVal;
        powerArray.add(powerVal);
        try {
          startTS = Tstamp.incrementMinutes(startTS, Integer.parseInt(args[6]));
        }
        catch (Exception e) {
          return "Day is not properly formatted.";
        }
      }
      if (0 == args[8].compareTo("average")) {
        output += totalPower / powerArray.size() + cr;
      }
      else if (0 == args[8].compareTo("max")) {
        output += Collections.max(powerArray) + cr;
      }
      else if (0 == args[8].compareTo("min")) {
        output += Collections.min(powerArray) + cr;
      }
      else {
        return "Statistic is not properly formatted.";
      }
    }
    catch (MiscClientException mce) {
      error = true;
      if (error) {
        return CON_NOT_FOUND;
      }
    }
    catch (NotAuthorizedException nae) {
      error = true;
    }
    catch (ResourceNotFoundException rne) {
      return RES_NOT_FOUND;
    }
    catch (BadXmlException bxe) {
      error = true;
    }
    return output;
  }

}
